package com.sikaryofficial.backend.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.domain.dto.req.AddressUserListReq;
import com.sikaryofficial.backend.domain.dto.req.AddressUserReq;
import com.sikaryofficial.backend.domain.dto.resp.AddressUserListDTO;
import com.sikaryofficial.backend.manager.UserAddressManager;
import com.sikaryofficial.backend.service.user.UserCacheService;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 我的地址Controller
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/user/address")
@Api(tags = "用户收货地址服务")
public class UserAddressController extends BaseController {
    @Autowired
    private UserAddressManager addressUserManager;
    @Autowired
    private UserCacheService userCacheService;

    /**
     * 查询我的地址列表
     */
    @PostMapping("/listPage")
    @ApiOperation("查询我的收货地址分页列表")
    public PageResult<AddressUserListDTO> listPage(@Validated @RequestBody AddressUserListReq req) {
        IPage<AddressUserListDTO> result = addressUserManager.listPage(req);
        return PageUtil.success(result.getRecords(), result.getTotal());
    }

    /**
     * 保存我的收货地址
     */
    @Log(title = "我的地址", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ApiOperation("保存我的收货地址")
    public HiResult<Boolean> saveOrUpdate(@Validated @RequestBody AddressUserReq req) {
        // 缓存用户经纬度
        if(CharSequenceUtil.isNotBlank(req.getLongitude()) && CharSequenceUtil.isNotBlank(req.getLatitude())){
            userCacheService.setUserLocalInfo(Double.parseDouble(req.getLongitude()), Double.parseDouble(req.getLatitude()));
        }
        return HiResult.success(addressUserManager.saveAddress(req));
    }

    /**
     * 删除我的收货地址
     */
    @Log(title = "我的地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userAddressId}")
    @ApiOperation("删除我的收货地址")
    public HiResult<Boolean> deleteUserAddress(@PathVariable("userAddressId") Long userAddressId) {
        if (userAddressId == null) {
            return HiResult.error("userAddressId is empty.");
        }
        return HiResult.success(addressUserManager.deleteUserAddress(userAddressId, SecurityUtils.getUserId()));
    }
}
